package hk.org.ha.cdars.job;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import hk.org.ha.cdars.datacontext.BisDataContext.G1CriterialNode;
import hk.org.ha.cdars.datacontext.BisDataContext.G1RefComboListCheckResult;
import hk.org.ha.cdars.datacontext.BisDataContext.IpStep2ComboList;
import hk.org.ha.cdars.job.CheckJob.Flag;
import lava.rt.common.LangCommon;
import lava.rt.common.UtilCommon;

@Service
public class G1RefComboListCheckJob extends CheckJob<G1RefComboListCheckResult>{

	@Override
	public List<G1RefComboListCheckResult> check() throws Exception {
		
		// TODO Auto-generated method stub
		
		List<G1RefComboListCheckResult> rets=new ArrayList<>();
		
		List<IpStep2ComboList> comboLists=dataContext.IP_STEP2_COMBO_LIST.selectList("",CR.nodeType.asc);
		
		Map<String, IpStep2ComboList> columnComboMap=UtilCommon.toMap(comboLists, (col)->{
			 return col.getPath();
		});
		List<String> diffMsgs=new ArrayList<>();
		dataContext.G1_CRITERIAL_NODE.selectCursoring((index,g1node)->{
			diffMsgs.clear();
			G1RefComboListCheckResult ret=new G1RefComboListCheckResult();
			IpStep2ComboList combo=columnComboMap.get(g1node.getPath());
			if(combo==null) {
				   ret.setFlag(Flag.不存在.code);	
				}else if(compare(g1node, combo, diffMsgs)>0) {
					ret.setFlag(Flag.差異.code);
					ret.setRemark(String.join(";", diffMsgs));
					ret.setDescription(combo.getDescription());
				}
				else {
					ret.setFlag(Flag.相同.code);	
					ret.setDescription(combo.getDescription());
				}
			ret.setG1TreeType(g1node.getTreeType());
			ret.setG1NodeType(getNodeType(g1node));
			ret.setG1ColumnId(g1node.getColumnId());
			ret.setG1Path(g1node.getPath());
			ret.setJobNo(getJobNo());
			
			rets.add(ret);
			
			return 0;
		}, CR.remark.notEq("warning"), CR.criteriaType.asc);
		
		return rets;
	}

	

}
